package newXgame.hall.object
{
	import flash.display.Sprite;
	
	import newXgame.hall.csuper.SuperRoomTable;
	import newXgame.hall.events.TableEvent;
	import newXgame.hall.object.background.TablesBackground;
	import newXgame.hall.object.tables.FivePlayerTable;
	import newXgame.hall.object.tables.FourPlayerTable;
	import newXgame.hall.object.tables.SixPlayerTable;
	import newXgame.hall.object.tables.ThreePlayerTable;
	import newXgame.hall.object.tables.TwoPlayerTable;
	import newXgame.utils.ExScrollUtils;
	
	import org.dstart.draw.csuper.CSprite;

	public class TableListObject extends CSprite
	{
		private var tableClass:Number;		//	桌子种类
		private var tableMax:Number;		//	桌子上限
		
		private var tableArray:Array;			//	桌子数组
		
		private var mcBg:TablesBackground;		//	桌子背景
		private var mcScrollH:ExScrollUtils;	//	竖向滑动条
		
		private var sprObject:Sprite;		//	桌子列表显示层
		private var sprMask:Sprite; 		//	桌子列表遮照层
		
		private var maxWidth:Number;
		private var maxHeight:Number;
		
		private const xSpace:Number = 40;
		private const ySpace:Number = 40;		
		
		public function TableListObject()
		{
			tableClass = 1;
			tableMax = 60;
			
			maxWidth = 300;		//	初始宽度 300 
			maxHeight = 300;	//	初始高度 300 
			
			mcBg = new TablesBackground();
			mcScrollH = new ExScrollUtils();
			sprObject = new Sprite();
			sprMask = new Sprite();
			
			sprObject.x = sprMask.x = 6;
			sprObject.y = sprMask.y = 17;
			
			mcScrollH.SetSprites(sprObject, sprMask);
			
			ChangeSize(maxWidth, maxHeight);
			CreateAllTable(TableEvent.FOUR_PLAYER, 60);
			
			this._root.addChild(mcBg);
			this._root.addChild(sprObject);
			this._root.addChild(sprMask);
			this._root.addChild(mcScrollH);
		}
		
		//	调整对象的大小
		public function ChangeSize(ww:Number, hh:Number):void
		{
			maxWidth = ww;
			maxHeight = hh;
			 
			this.mcBg.ChangeSize(ww, hh);
			this.mcScrollH.ChangeHeight(hh - 35);
			this.mcScrollH.SetPlace(ww - this.mcScrollH.width - 4, 14);
			
			this.sprMask.graphics.clear();
			this.sprMask.graphics.beginFill(0xFF0000, 0);
			this.sprMask.graphics.drawRect(0, 0, ww - 36, hh - 45);
			this.sprMask.graphics.endFill();
			
			RushTablePlace();
						
			this.mcScrollH.RushVisible();
		}
		
		//	创建房间列表
		public function CreateAllTable(tClass:Number, tMax:Number):void
		{
			var i:int, j:int;
			var tObj:SuperRoomTable;
			
			for(i = 0; i < this.sprObject.numChildren; i++)
			{
				tObj = this.sprObject.getChildAt(i) as SuperRoomTable;
				if(tObj)
				{
					tObj.parent.removeChild(tObj);
				}
			}
			
			this.tableArray = new Array();
			
			for(i = 0; i < tMax; i++)
			{
				switch(tClass)
				{
					case TableEvent.TWO_PLAYER :
						tObj = new TwoPlayerTable();
						break;
						 
					case TableEvent.THREE_PLAYER :
						tObj = new ThreePlayerTable();
						break;
						 
					case TableEvent.FOUR_PLAYER :
						tObj = new FourPlayerTable();
						break;
						 
					case TableEvent.FIVE_PLAYER :
						tObj = new FivePlayerTable();
						break;
						 
					case TableEvent.SIX_PLAYER :
						tObj = new SixPlayerTable();
						break;
				}

				if(tObj)
				{
					var xNum:Number = Math.floor((this.sprMask.width - 4 - xSpace) / (tObj.width + xSpace));					
					
					tObj.x = xSpace + (i % xNum) * (tObj.width + xSpace);
					tObj.y = ySpace + Math.floor(i / xNum) * (tObj.height + ySpace);
					 
					this.tableArray.push(tObj);
					this.sprObject.addChild(tObj);
				}						 
			}
		}
		
		private function RushTablePlace():void
		{
			if(this.tableArray)
			{			
				var isTrace:Boolean = false;	
				var xNum:Number;
				for(var i:int = 0; i < this.tableArray.length; i++)
				{
					var tObj:SuperRoomTable = this.tableArray[i];
					if(tObj)
					{						
						if(!xNum)
						{
							xNum = Math.floor((this.sprMask.width + 1 - xSpace) / (tObj.width + xSpace));
							
							this.sprObject.graphics.clear();
							this.sprObject.graphics.beginFill(0xFF0000, 0);
							this.sprObject.graphics.drawRect(0, 0, this.sprMask.width, Math.floor(this.tableArray.length / xNum) * (tObj.height + ySpace) + ySpace);
							this.sprObject.graphics.endFill();
						}
						
						tObj.x = xSpace + (i % xNum) * (tObj.width + xSpace);
						tObj.y = ySpace + Math.floor(i / xNum) * (tObj.height + ySpace);						
					}
				}
			}
		}
	}
}